#include<bits/stdc++.h>
using namespace std;

#define int long long
#define F(i,a,b) for(int i=a;i<=b;i++)

int read(){int x=0,f=0;char ch=getchar();while(ch<48||ch>57){f|=(ch=='-');ch=getchar();}while(ch>=48&&ch<=57){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return f?-x:x;}
void write(int x){int _num=0;char ch[101];if(x<0){x=-x;putchar('-');}do{ch[++_num]=x%10+48;x/=10;}while(x>0);while(_num){putchar(ch[_num--]);}}

int t,n,a[200001],last[1000001],pre[200001],f[200001][2][2],sum[200001];

signed main()
{
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    t=read();
    while(t--)
    {
        n=read();
        memset(last,0,sizeof(last));
        memset(pre,0,sizeof(pre));
        memset(sum,0,sizeof(sum));
        F(i,1,n)
        {
            a[i]=read();
            pre[i]=last[a[i]];
            last[a[i]]=i;
            f[i][0][0]=f[i][0][1]=f[i][1][0]=f[i][1][1]=0;
        }
        F(i,2,n)
        {
            sum[i]=sum[i-1];
            if(a[i]==a[i-1])
            {
                sum[i]+=a[i];
            }
        }
        F(i,2,n)
        {
            f[i][0][0]=f[i][1][0]=max(f[i-1][0][0],f[i-1][0][1]);
            f[i][1][1]=f[i][0][1]=max(f[i-1][1][0],f[i-1][1][1]);
            if(a[i]==a[i-1])
            {
                f[i][0][0]+=a[i];
                f[i][1][1]+=a[i];
            }
            int j=pre[i];
            if(j==i-1)
            {
                j=pre[j];
            }
            if(j>0)
            {
                f[i][0][1]=max(f[i][0][1],f[j+1][1][0]+sum[i-1]-sum[j+1]+a[i]);
                f[i][1][0]=max(f[i][1][0],f[j+1][0][1]+sum[i-1]-sum[j+1]+a[i]);
            }
        }
        write(max(max(f[n][0][0],f[n][0][1]),max(f[n][1][0],f[n][1][1])));
        putchar('\n');
    }
    return 0;
}
